package com.yishusmt.mobsmt.serviceimpl;

import com.yishusmt.mobsmt.constant.RedisConstant;
import com.yishusmt.mobsmt.constant.VerificationConstant;
import com.yishusmt.mobsmt.dto.base.Result;
import com.yishusmt.mobsmt.dto.request.ResetPasswordReq;
import com.yishusmt.mobsmt.dto.response.Tips;
import com.yishusmt.mobsmt.dto.response.UserRes;
import com.yishusmt.mobsmt.entity.UserEntity;
import com.yishusmt.mobsmt.enums.ResultCodeEnum;
import com.yishusmt.mobsmt.mapper.UserEntityMapper;
import com.yishusmt.mobsmt.mapper.UserInfoMapper;
import com.yishusmt.mobsmt.service.PasswordService;
import com.yishusmt.mobsmt.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.yishusmt.mobsmt.constant.SmsConstant.SMS_STATUS;
import static com.yishusmt.mobsmt.constant.VerificationConstant.EMAIL;
import static com.yishusmt.mobsmt.constant.VerificationConstant.PHONE;

/**
 * 用户密码找回
 *
 * @author zhangyi
 * @date 2018/8/16 11:06
 */
@Service
public class PasswordServiceImpl implements PasswordService {


    @Autowired
    private MailUtil mailUtil;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserEntityMapper userEntityMapper;

    /**
     * 找回密码
     *
     * @param style   1: 手机号  0: 邮箱
     * @param contact
     * @return
     */
    @Override
    public Result findPassword(String style, String contact) {

        //查找当前是否存在
        UserRes userRes = EMAIL.equals(style)
                ? userInfoMapper.selectUserByEmail(contact): userInfoMapper.selectUserSelective(contact);
        if(userRes == null){
            return TipsUtil.returnTips(ResultCodeEnum.OPERATION_FAIL, "用户不存在！");
        }
        String code = redisUtil.get(RedisConstant.FIND_PASSWORD + contact);

        if (code != null) {
            return TipsUtil.returnTips(ResultCodeEnum.CODE_ERROR, "验证码已经存在");
        } else {
            //创建验证码
            String randCode = RandCodeUtil.getRandCode();

            String sms = "";
            //发送验证码
            if (EMAIL.equals(style)) {
                mailUtil.sendMessage(contact, "易树智造", "发送给您的重置密码验证码：" + randCode);
            } else if (PHONE.equals(style)) {
                sms = AliyunSmsUtil.sendCodeSms(contact, randCode).getCode();
            }
            if(SMS_STATUS.equals(sms)){
                //存入缓存
                redisUtil.set(RedisConstant.FIND_PASSWORD + contact, randCode, new Long(3 * 60));
                return TipsUtil.returnTips(ResultCodeEnum.CODE_SUCCESS, "验证码发送成功！");
            }else{
                return TipsUtil.returnTips(ResultCodeEnum.OPERATION_FAIL, "短信发送过多！");
            }

       }
    }

    /**
     * 重置密码
     *
     * @param resetPasswordReq
     * @return
     */
    @Override
    public Result resetPassword(ResetPasswordReq resetPasswordReq) {
        //验证缓存中是否存在
        String code = resetPasswordReq.getCode();
        String phone = resetPasswordReq.getPhone();
        String redisCode = redisUtil.get(RedisConstant.FIND_PASSWORD + phone);
        if(code.equals(redisCode)){
            redisUtil.del(RedisConstant.FIND_PASSWORD+phone);
            UserRes userRes = userInfoMapper.selectUserSelective(phone);
            UserEntity userEntity = new UserEntity();
            userEntity.setId(userRes.getId());
            userEntity.setPassword(MD5Util.encryptString(resetPasswordReq.getNewPassword()));
            userEntity.setUpdateTime(DateUtil.getCurrentDate());
            int i = userEntityMapper.updateByPrimaryKeySelective(userEntity);
            return 0==i?TipsUtil.returnTips(ResultCodeEnum.OPERATION_FAIL,"修改密码失败！")
                    : TipsUtil.returnTips(ResultCodeEnum.OPERATION_SUCCESS,"修改密码成功");
        }else{
            return TipsUtil.returnTips(ResultCodeEnum.OPERATION_FAIL,"重置密码失败！");
        }
    }
}
